package com.stox.ui.javafx.core.animation;

import java.math.BigDecimal;
import java.util.Timer;
import java.util.TimerTask;

import javafx.animation.FadeTransition;
import javafx.animation.Transition;
import javafx.application.Platform;
import javafx.geometry.Rectangle2D;
import javafx.scene.Node;
import javafx.stage.Stage;
import javafx.util.Duration;


public class Transitions{
	
	private static double duration = 0.3;
	
	public static Transition getTransition(Node node, double duration, double fromX, double fromY, double toX, double toY) {
		FadeTransition fadeTransition = new FadeTransition();
		fadeTransition.setDuration(Duration.seconds(duration));
		fadeTransition.setNode(node);
		fadeTransition.setFromValue(fromX);
		fadeTransition.setToValue(toX);
		return fadeTransition;
	}
	
	public static Transition getClose(Node node) {
		return getTransition(node, duration, 1, 1, 0, 0);
	}
	
	public static Transition getMinimize(Node node) {
		return getTransition(node, duration, 1, 1, 0, 0);
	}
	
	public static Transition getOpen(Node node) {
		return getTransition(node, duration, 0, 0, 1, 1);
	}
	
	public static Transition getRestore(Node node) {
		return getTransition(node, duration, 0, 0, 1, 1);
	}
	
	public static void resize(final Stage stage, final Rectangle2D rectangle){
		Timer animTimer = new Timer();
		final BigDecimal divisions = new BigDecimal(40);
		final BigDecimal xTick = new BigDecimal(stage.getX()).add(new BigDecimal(rectangle.getMinX()).negate()).divide(divisions);
		final BigDecimal yTick = new BigDecimal(stage.getY()).add(new BigDecimal(rectangle.getMinY()).negate()).divide(divisions);
		final BigDecimal widthTick = new BigDecimal(stage.getWidth()).add(new BigDecimal(rectangle.getMaxX()).negate().add(new BigDecimal(rectangle.getMinX()).negate())).divide(divisions);
		final BigDecimal heightTick = new BigDecimal(stage.getHeight()).add(new BigDecimal (rectangle.getMaxY()).negate().add(new BigDecimal(rectangle.getMinY()).negate())).divide(divisions);
		animTimer.scheduleAtFixedRate(new TimerTask(){
			@Override
	        public void run(){
				Platform.runLater(new Runnable(){
					@Override
			        public void run(){
						if(stage.getX() != rectangle.getMinX())
							stage.setX(new BigDecimal(stage.getX()).add(xTick.negate()).doubleValue());
						if(stage.getY() != rectangle.getMinY())
							stage.setY(new BigDecimal(stage.getY()).add( yTick.negate()).doubleValue());
						if(stage.getX()+stage.getWidth() != rectangle.getMaxX())
							stage.setWidth(new BigDecimal(stage.getWidth()).add(widthTick.negate()).doubleValue());
						if(stage.getY() + stage.getHeight() != rectangle.getMaxY())
							stage.setHeight(new BigDecimal(stage.getHeight()).add(heightTick.negate()).doubleValue());
						if(stage.getX() == rectangle.getMinX()
								&& stage.getY() == rectangle.getMinY()
								&& stage.getX()+stage.getWidth() == rectangle.getMaxX()
								&& stage.getY() + stage.getHeight() == rectangle.getMaxY()){
							cancel();
						}
					}
				});
			}
		}, 0, 10);
	}
	
	
}
